﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.Temp
{
	internal partial class ExcelProgressBar : System.Windows.Forms.Form
	{
		public bool Succeed { get; set; }

		public ExcelProgressBar()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.CenterToParent();
			this.Succeed = false;
			this.m_BkWorker = new BackgroundWorker();
			this.m_BkWorker.WorkerReportsProgress = true;
			this.m_BkWorker.WorkerSupportsCancellation = true;
			this.m_BkWorker.DoWork += this.excelWorker_DoWork;
			this.m_BkWorker.ProgressChanged += this.excelWorker_ProgressChanged;
			this.m_BkWorker.RunWorkerCompleted += this.excelWorker_RunWorkerCompleted;
		}

		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			this.m_BkWorker.RunWorkerAsync();
		}

		public ExcelProgressBar(HYDuctSystemDocument doc) : this()
		{
			this.m_Document = doc;
		}

		private void excelWorker_DoWork(object sender, DoWorkEventArgs e)
		{
			BackgroundWorker bkWorker = sender as BackgroundWorker;
			this.Succeed = this.m_Document.OnExcel(bkWorker);
		}

		private void excelWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			this.progressBar.Value = e.ProgressPercentage;
			if (e.UserState != null)
			{
				this.labelMessage.Text = e.UserState.ToString();
			}
		}

		private void excelWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			base.Close();
		}

		private HYDuctSystemDocument m_Document;

		private BackgroundWorker m_BkWorker;
	}
}
